import * as THREE from 'three';
import Model from './model' 
import Preloader from './preloader'
import OrbitControls from 'three-orbitcontrols';
/**
 * 控制类，入口
 * 主要用于全局控制及各种交互事件创建
 */
export default class Controls{
    // 01: Build controller
    constructor() {
        console.log('控制器类')
        // User viewing modes
        this.introComplete = false; // Tracks intro or main view 介绍界面或主界面
        this.zoom = 1;
        TweenLite.defaultEase = Power2.easeInOut;
        this.pageMain = document.getElementById("pageMain");
        this.model = new Model(this.pageMain); // 设置场景
        window.addEventListener("resize", this.onWindowResize.bind(this), false);
        this.preload();
    }
    /////////////////////////////////////// INTRO SEQUENCE ////////////////////////////////////////
    // 02: Start asset preload 预加载资源
    preload = function () {
        var manifesto = [
            // Cube textures
            { name: "envReflection", type: "cubetexture", ext: "jpg" }, // 远处环境
            { name: "envSkybox", type: "cubetexture", ext: "jpg" }, // 天空场景

            // 仓库
            { name: "warehouse/biaoyu", type: "texture", ext: "png" }, // 标语
            { name: "warehouse/box", type: "texture", ext: "png" }, 
            { name: "warehouse/door_left", type: "texture", ext: "png" }, 
            { name: "warehouse/door_right", type: "texture", ext: "png" }, 
            { name: "warehouse/floor", type: "texture", ext: "jpg" }, 
            { name: "warehouse/line", type: "texture", ext: "png" }, 
            { name: "warehouse/plane", type: "texture", ext: "png" }, 
            { name: "warehouse/rack", type: "texture", ext: "png" }, 
            { name: "warehouse/roll", type: "texture", ext: "png" }, 
            { name: "warehouse/window", type: "texture", ext: "png" }, 
            { name: "warehouse/rack", type: "meshLambert", ext: "png" }, 
            { name: "warehouse/box", type: "meshLambert", ext: "png" }, 
        ];
        var path = "/dist/images/3d/";
        this.preloader = new Preloader(path, manifesto, this);
        window["preloader"] = this.preloader;
        this.preloader.start();
        this.model.introPreloading();
    };
    // 03: 预加载结束、新建车辆天空等、进入引导页、等待点击事件
    preloadComplete = function (_cargo) {
        this.introAnimation();
        this.model.introPreloaded(_cargo);
        this.introComplete = true;
        
    };
    // 04: 点击引导页，切换摄像头角度，移除预加载页面
    introAnimation = function (evt) {
        // 引导页切换动画
        this.model.introStart();
        this.preloader.remove();
        this.initControls();
    };
    // 05: 引导页动画结束后，初始化控制器
    initControls = function (evt) {

        this.controls = new OrbitControls(this.model.cam.camera, this.pageMain);
        this.controls.enableDamping = true;
        this.controls.dampingFactor = 0.5;
        // 视角最小距离
        this.controls.minDistance = 0;
        // 视角最远距离
        this.controls.maxDistance = 1000;
        // 最大角度
        this.controls.maxPolarAngle = Math.PI / 2.2;
        // 轨迹控制器固定角度
        this.controls.target = new THREE.Vector3(1,100,0);
    };
    
    /////////////////////////////////////// WINDOW EVENTS ////////////////////////////////////////

    // Browser window resize 
    onWindowResize = function () {
        this.model.onWindowResize();
    };
    update = function (t) {
        if (this.introComplete === false) {
            this.controls && this.controls.update();
            this.model.updateIntro(t);
        }
        else {
            this.model.update(t, false);
        }
    };
}